<template>
  <div>
    <el-dialog
      title="审核"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="800px"
      @closed="resetForm('form')"
      :lock-scroll="false"
    >
      <div>
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="审批状态">
            <el-radio-group v-model="form.approverStatus">
              <el-radio label="2">同意</el-radio>
              <el-radio label="3">驳回</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="审批意见">
            <el-input
              type="textarea"
              :rows="10"
              placeholder="请输入审批意见"
              v-model="form.approverOpinion"
            >
            </el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" v-preventReClick @click="submitForm('form')"
          >提 交</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      dialogVisible: false,
      checkedList: [],
      form: {
        id: '', //流程实例
        approverOpinion: '', //审批意见
        approverStatus: '2', //审批状态，2-同意，3-不同意
        instanceId: '',
      },
    }
  },
  mounted() {
    this.$bus.$on('showDialogAudit', (v) => {
      if (v.length == 0) return
      this.checkedList = this.deepClone(v)
      this.dialogVisible = true
    })
  },
  beforeDestroy() {
    this.$bus.$off('showDialogAudit')
  },
  methods: {
    submitForm(form) {
      this.$refs[form].validate(async (valid) => {
        if (valid) {
          let list = []
          this.checkedList.forEach((item) => {
            this.form.id = item.id
            this.form.instanceId = item.instanceId
            list.push(this.auditFun(this.form, item))
          })
          Promise.all(list)
            .then((res) => {
              this.$bus.$emit('refeshFlowRunTask')
              this.successMsg('操作成功')
              this.dialogVisible = false
            })
            .catch((e) => {})
        }
      })
    },
    async auditFun(formObj, item) {
      return new Promise(async (res) => {
        let val1, val2
        val1 = await this.$store.dispatch('completeFlowApi', formObj)
        //根据返回值（true 流程走完|false 流程未走完）修改auditStatus状态  2-通过  3-未通过
        if (val1.data[0]) {
          let obj = {
            id: item.initData.id,
            auditStatus: formObj.approverStatus,
            status: formObj.approverStatus == 2 ? 1 : 0,
          }
          val2 = await this.$store.dispatch('updateNewsContents', obj)
        }
        res(val2)
      })
    },
    //重置表单
    resetForm(form) {
      this.$refs[form].resetFields()
      this.form.id = ''
      this.form.approverOpinion = ''
      this.form.approverStatus = '2'
    },
  },
}
</script>
<style scoped>
.investigate {
  box-sizing: border-box;
  padding-left: 80px;
}
</style>